package ru.spb.osll.myfit.client.widget;

import java.util.Vector;

import ru.spb.osll.myfit.common.PageElement;
import ru.spb.osll.myfit.common.Table;
import ru.spb.osll.myfit.common.TableCell;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;

public class FixedTableWidget extends TableWidget {

  private int m_columnCount = 0;

  public FixedTableWidget(GWTWidgetFactory gwtWidgetFactory,Table table) {
    super(gwtWidgetFactory, table);

    if (table.getData().size() > 1)
      m_columnCount = table.getData().elementAt(1).size();

    for (int i = 0; i < m_columnCount; ++i)
      refreshColumn(i);
  }

  protected void addRow(int row, Vector<TableCell> data) {
    for (int j = 0; j < data.size(); ++j) {
      TextBox tb = createTextEdit(data.elementAt(j));
      if (row <= 1) {
        tb.addStyleName("headerCellStyle");
        tb.setReadOnly(true);
      }
      m_flexTable.setWidget(row, j, tb);
    }
  }

  protected void addNewRow(int row) {
    if (row > 1) {
      m_flexTable.insertRow(row);
      for (int j = 0; j < m_columnCount; ++j) {
        TextBox tb = createTextEdit(new TableCell());
        tb.setVisibleLength(getMaxLength(j));
        m_flexTable.setWidget(row, j, tb);
      }
    }
  }

  @Override
  protected void addNewCell(int row, int cell) {
    // not applicable
  }

  @Override
  protected void deleteCell(int row, int cell) {
    // not applicable
  }

  public PageElement save() {
    Table table = new Table(getTableStyle());
    // table.setFixtureClassName(getFixtureClassName());
    int rowCount = m_flexTable.getRowCount();
    for (int i = 0; i < rowCount; ++i) {
      Vector<String> line = new Vector<String>();
      int cellCount = m_flexTable.getCellCount(i);
      for (int j = 0; j < cellCount; ++j)
        try {
          String text = ((TextBox) m_flexTable.getWidget(i, j)).getText();
          text = (text.isEmpty()) ? " " : text;
          line.addElement(text);
        } catch (Exception ex) {
        }
      if (!line.isEmpty()) {
        Vector<TableCell> lineCell = new Vector<TableCell>();
        for (String item : line) {
          TableCell tableCell = new TableCell();
          tableCell.setText(item);
          lineCell.add(tableCell);
        }
        table.addLine(lineCell);
      }
    }
    return table;
  }

  protected void setReadOnly(boolean isReadOnly) {
    int rowCount = m_flexTable.getRowCount();
    for (int i = 0; i < rowCount; i++) {
      int cellCount = m_flexTable.getCellCount(i);
      for (int j = 0; j < cellCount; ++j) {
        try {
          ((TextBox) m_flexTable.getWidget(i, j)).setReadOnly(isReadOnly ||
              (i < 2));
        } catch (Exception ex) {
        }
      }
    }
  }

  protected int getMaxLength(int column) {
    int maxLength = 0;
    int rowCount = m_flexTable.getRowCount();
    for (int i = 0; i < rowCount; ++i) {
      int cellCount = m_flexTable.getCellCount(i);
      if (column < cellCount) {
        int curLength = ((TextBox) m_flexTable.getWidget(i, column)).getText()
            .length();
        maxLength = (curLength > maxLength) ? curLength : maxLength;
      }
    }
    return maxLength;
  }

  protected void changeWidth(TextBox tb, int displace) {
    if (!tb.isReadOnly()) {
      int length = tb.getText().length() + displace;
      int maxLength = getMaxLength(m_currentCell);

      if ((length >= getMaxLength(m_currentCell)) && (maxLength > 0)) {
        int rowCount = m_flexTable.getRowCount();
        for (int i = 0; i < rowCount; ++i) {
          int cellCount = m_flexTable.getCellCount(i);
          if (m_currentCell < cellCount) {
            ((TextBox) m_flexTable.getWidget(i, m_currentCell))
                .setVisibleLength(maxLength + displace);
          }
        }
        tb.setText(tb.getText());
      }
    }
  }

  public void removeRow(int row) {
    if (row >= 2) {
      m_flexTable.removeRow(row);
    }
  }

  private void refreshColumn(int column) {
    int rowCount = m_flexTable.getRowCount();
    for (int i = 0; i < rowCount; ++i) {
      int cellCount = m_flexTable.getCellCount(i);
      if (column < cellCount) {
        ((TextBox) m_flexTable.getWidget(i, column))
            .setVisibleLength(getMaxLength(column));
      }
    }
  }

  protected void refreshTable(FlexTable table) {
  }

  protected void tuneMenu() {
    m_addRowItem.setVisible(m_selectedRow >= 1);
    m_deleteRowItem.setVisible(m_selectedRow >= 2 &&
        m_flexTable.getRowCount() > 1);
  }

}
